﻿using Castle.MicroKernel.Registration;
using System.Reflection;
using Inovout.Business;
using Castle.Core;
using Inovout.Gui.Wpf.ViewModels;
using Inovout.Gui.Wpf.Views;
using System.Resources;
using System.Collections;
using System.Threading;

namespace Inovout.Gui.Wpf
{
    public class WpfInstaller : ComponentInstaller
    {
        protected override void Register()
        {

            Container.Register(
                Component.For<IViewDataDictionary>()
                .ImplementedBy<ViewDataDictionary>()
                .LifeStyle.Transient
               );

            Container.Register(
                Component.For(typeof(IViewDataDictionary<>))
                .ImplementedBy(typeof(ViewDataDictionary<>))
                .LifeStyle.Transient
                );

            //Container.Register(
            //           Component.For(typeof(IGuiAction))
            //           .ImplementedBy(typeof(GuiAction))
            //           );

            //Container.Register(
            //    Component.For<IMainWorkbench>().ImplementedBy<RibbonWorkbench>().Named(WpfApplication.MainWorkbenchKey)
            //    );
            //Container.Register(
            //      Component.For<IDialogWorkbench>().ImplementedBy<DialogWorkbench>().Named(WpfApplication.DialogWorkbenchKey).LifeStyle.Transient
            //      );

            //Container.Register(
            //  AllTypes.FromThisAssembly().BasedOn<MainWorkbenchViewModel>()
            //                             .WithService.Self()
            //                             .WithService.Base()
            //                             .WithService.AllInterfaces());
        }


        public override void Register(Assembly assembly)
        {
            InstallView(assembly);
            InstallViewModel(assembly);
        }

        private void InstallViewModel(Assembly assembly)
        {
            Container.Register(
                AllTypes.FromAssembly(assembly)
                    .BasedOn<BaseViewModel>()
                    .Configure(c => c.Named(c.Implementation.Name.ToLower()))
                , AllTypes.FromAssembly(assembly)
                    .BasedOn(typeof(ModelViewModel<>))
                    .WithService.Base()
                    .WithService.AllInterfaces()
                    .Configure(c =>
                        {
                            c.Named(c.Implementation.Name.ToLower());
                            c.OnCreate((kernel, instance) => {
                                ((BaseViewModel)instance).logger = Logger.Create(instance.GetType());
                            });
                        }
                 )
                , AllTypes.FromAssembly(assembly)
                    .BasedOn(typeof(ModelCollectionViewModel<>))
                    .WithService.AllInterfaces()
                    .Configure(c => c.Named(c.Implementation.Name.ToLower()))
                );
        }

        private void InstallView(Assembly assembly)
        {
            string resourceName = assembly.GetName().Name + ".g";
            if (assembly.GetManifestResourceInfo(resourceName + ".resources") != null)
            {
                ResourceManager rm = new ResourceManager(resourceName, assembly);
                ResourceSet resourceSet = rm.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, true);
                foreach (DictionaryEntry resource in resourceSet)
                {
                    string resourceKey=resource.Key.ToString();
                    //if (resourceKey.Substring(resourceKey.LastIndexOf('.') + 1).ToLower().Equals("baml"))
                    //{
                    //    WpfApplication.resourceDictionaryUris.Add(string.Format("/{0};component/{1}.xaml"
                    //    , assembly.GetName().Name, resourceKey.Substring(0, resourceKey.LastIndexOf('.'))));
                    //}
                }
                rm.ReleaseAllResources();
            }
            
            
            Container.Register(
            AllTypes.FromAssembly(assembly)
            .BasedOn<IView>()
            .Configure(c => c.Named(c.Implementation.Name.ToLower())
                .LifeStyle.Is(LifestyleType.Transient)
                )
             );
        }
    }
}
